package Java网络编程.TCP通信程序练习.p2_客户端数据来自键盘录入;

import java.io.*;
import java.net.Socket;

/*数据来自键盘录入，直到886停止*/
public class ClientDemo {
    public static void main(String[] args) throws IOException {
        Socket s = new Socket("192.168.189.1",2);

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        /*封装输出流对象
        *   ---为了与BufferedReader配套选择BufferedWriter
        *   ---若要使用BufferedWriter，需要将字节流转换为字符流*/
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));


        String line;
        while((line=br.readLine())!=null){ //String readLine() 读一行文字。
            if("886".equals(line)){
                break;
            }else{

                //按字节写入---使用字节流
                /*OutputStream os = s.getOutputStream();
                os.write((line.getBytes()));*/

                //按字符串写入---使用字符流
                bw.write(line);
                bw.newLine(); //void newLine() 写一行行分隔符。
                bw.flush(); //void flush() 刷新流。
            }
        }

        s.close();
    }
}
